#模拟银行系统
#功能:账户注册,root模式,密码保护,查询,存款,取款,贷款,投资
#更新:简单化
import json
import os
import time
import calendar
from msvcrt import getwch as m #仅Windows平台

with open("data.json","r") as data: #读入数据
    w=json.loads(data.read())
admit = False
while True:
    #登入界面
    os.system('cls')
    a=input("输入用户名(0退出银行):")
    if a == "0":
        break
    elif a == "root" and input()=="root":#root模式
        input(w)
        continue
    #如果此用户不存在,创建用户并设密码,并回到登入界面
    elif a not in w:
        w[a]=[input("新用户请设置密码:"),0]
        continue
    #如果存在,进入验证界面
    for times in [4,3,2,1]:
        if w[a][0] == input(f"输入'{a}'的密码:"):
            os.system('cls')
            print("登录成功!")
            admit = True
            break #退出验证循环
        else:
            print(f'密码不对!还有{times}次!')
    else:
        print(f"错误过多!退出'{a}'的登录!")
    while admit:
        mid=list(time.localtime(time.time()))
        print(calendar.month(mid[0],mid[1]))
        print("客户:",a,time.strftime("%Y-%m-%d %H:%M",time.localtime()))
        print(f"财产:{w[a][1]}")
        print("1.存取款 2.年贷 3.投资 x.账户 0.退出:")
        fla1 = m()
        os.system('cls')
        while fla1 == "1":
            try:
                amount = float(input("输入金额(带负号为取款):"))
            except ValueError:
                print("输入异常")
            else:
                w[a][1] += amount
                break
        while fla1 == "2":
            try:
                loan=float(input("贷款:"))
                rate=float(input("年利率(不带百分号的小数):"))
                pay=int(input("每年还款:"))
            except ValueError:
                print("输入异常!")
            else:
                if loan <= 0 or rate <= 0 or pay <= 0:
                    print("输入的数据有误")
                    continue
                while loan >0:
                    print(f"{loan:.2f},支付{min(pay,loan):.2f}")
                    loan -= min(pay,loan)
                    loan *= (1 + rate)
                break
        while fla1 == "3":
            try:
                inv = float(input("每月定投金额:\t "))
                rate = float(input("年收益率:\t "))
                year = int(input("定投期限(年):\t "))
            except ValueError:
                print("输入异常!")
            else:
                if inv <= 0 or rate <= 0 or year <= 0:
                    print("输入的数据有误")
                    continue
                inv = 12 * inv
                rate = rate / 100
                if rate == 0:
                    expected = 0
                else:
                    expected = inv*(1+rate)*(pow((1+rate),year)-1)/rate
                    print(f"定投的预期收入为:{expected}" )
                    break
        if fla1 == "x":
            os.system("cls")
            print("1.修改密码 2.删除账号")
            fla2 = m()
            if fla2 == "1" and w[a][0] == input("输入原来密码:"):
                w[a][0]=input("输入新的密码:")
            elif fla2 == "2":
                w.pop(a)
            admit = False
        elif fla1 == "0":
            admit = False
with open("data.json","w") as data:
    data.write(json.dumps(w)) #写入数据